<?php
/**
 * @file 3gplex/includes/tools.inc
 *
 * Low level UBICAST/3GPLEX API.
 */
require_once('./'. drupal_get_path('module', 'drupalplex') .'/includes/module.inc');

class plex_system extends plex_module
{
	const description = "Configures and loads API for Drupal core system module: Website settings";
	function get_description() { return self::description; }
	protected $suggested = array( 'taxonomy' );
	/**
	 * 
	 */
	protected function apply_settings($settings)
	{
		if (isset($settings['clean_urls']))
			variable_set('clean_urls', $settings['clean_urls']);
		if (isset($settings['theme_default']))
			variable_set('theme_default', $settings['theme_default']);
		if (isset($settings['name']))
			variable_set('site_name', $settings['name']);
		if (isset($settings['mail']))
			variable_set('site_mail', $settings['mail']);
		if (isset($settings['slogan']))
			variable_set('site_slogan', $settings['slogan']);
		if (isset($settings['site_mission']))
			variable_set('mission', $settings['mission']);
		if (isset($settings['footer']))
			variable_set('site_footer', $settings['footer']);
		if (isset($settings['anonymous']))
			variable_set('anonymous', $settings['anonymous']);
		if (isset($settings['frontpage']))
			variable_set('site_frontpage', $settings['frontpage']);
			
		if (isset($settings['search_cron_limit']))
			variable_set('search_cron_limit', $settings['search_cron_limit']);
		if (isset($settings['minimum_word_size']))
			variable_set('minimum_word_size', $settings['minimum_word_size']);
		if (isset($settings['node_rank_relevance']))
			variable_set('node_rank_relevance', $settings['node_rank_relevance']);
		if (isset($settings['node_rank_recent']))
			variable_set('node_rank_recent', $settings['node_rank_recent']);
		if (isset($settings['node_rank_comments']))
			variable_set('node_rank_comments', $settings['node_rank_comments']);
			
		if (isset($settings['cache']))
			variable_set('cache', $settings['cache']);
		if (isset($settings['cache_lifetime']))
			variable_set('cache_lifetime', $settings['cache_lifetime']);
		if (isset($settings['preprocess_css']))
			variable_set('preprocess_css', $settings['preprocess_css']);

		if (!isset($settings['modules']) || !is_array($settings['modules']))
			return true;
		foreach ((array)$settings['modules'] as $module) {
			require_modules($module);
		}
	}

	/**
	 * 
	 */
	protected function validate_settings(array &$settings)
	{
		if (isset($settings['modules']) && !is_array($settings['modules'])) {
			plex::warn("Cannot install required modules as list is not well formed");
			return false;
		}
		return true;
	}

	/**
	 * 
	 */
	protected function build_settings($xml)
	{
		$settings = array();
		if (isset($xml->clean_urls))
			$settings['clean_urls'] = (int)(string)$xml->clean_urls;
		if (isset($xml->theme_default))
			$settings['theme_default'] = (string)$xml->theme_default;
		if (isset($xml->name))
			$settings['name'] = (string)$xml->name;
		if (isset($xml->mail))
			$settings['mail'] = (string)$xml->mail;
		if (isset($xml->slogan))
			$settings['slogan'] = (string)$xml->slogan;
		if (isset($xml->mission))
			$settings['mission'] = (string)$xml->mission;
		if (isset($xml->footer))
			$settings['footer'] = (string)$xml->footer;
		if (isset($xml->anonymous))
			$settings['anonymous'] = (string)$xml->anonymous;
		if (isset($xml->frontpage))
			$settings['frontpage'] = (string)$xml->frontpage;
			
		if (isset($xml->search_cron_limit))
			$settings['search_cron_limit'] = (int)(string)$xml->search_cron_limit;
		if (isset($xml->minimum_word_size))
			$settings['minimum_word_size'] = (int)(string)$xml->minimum_word_size;
		if (isset($xml->node_rank_relevance))
			$settings['node_rank_relevance'] = (int)(string)$xml->node_rank_relevance;
		if (isset($xml->node_rank_recent))
			$settings['node_rank_recent'] = (int)(string)$xml->node_rank_recent;
		if (isset($xml->node_rank_comments))
			$settings['node_rank_comments'] = (int)(string)$xml->node_rank_comments;
			
		if (isset($xml->cache))
			$settings['cache'] = (int)(string)$xml->cache;
		if (isset($xml->cache_lifetime))
			$settings['cache_lifetime'] = (int)(string)$xml->cache_lifetime;
		if (isset($xml->preprocess_css))
			$settings['preprocess_css'] = (int)(string)$xml->preprocess_css;

		if (!isset($xml->modules) || !isset($xml->modules->module))
			return $settings;
		$settings['modules'] = array();
		foreach ((array)$xml->modules->module as $module) {
			$settings['modules'][] = $module;
		}
		return $settings;
	}
	
}

